#include "lvgl/lvgl.h"
#include <stdio.h>
#include <time.h>

/*Create a button descriptor string array*/
static const char * btnm_map[] = {LV_SYMBOL_PREV, LV_SYMBOL_HOME, LV_SYMBOL_NEXT,""};

static struct {
    lv_obj_t* btnm;
} NavigationBtnCtrl;

typedef enum {
    BAT_ICON,
    WIFI_ICON,
    GPS_ICON,
    USB_ICON,
    BLE_ICON,
    ICON_NUM,
}StatusBar_icon;

const char* icon_table[ICON_NUM] = {
    LV_SYMBOL_BATTERY_FULL,
    LV_SYMBOL_WIFI,
    LV_SYMBOL_GPS,
    LV_SYMBOL_USB,
    LV_SYMBOL_BLUETOOTH,
};

static struct {
    lv_obj_t* obj;
    lv_obj_t* line;
    unsigned char sw_mask;
} StatusBarCtrl;

static lv_obj_t* clock_label;
static lv_style_t bar_status_style;
static lv_style_t main_style;

static void timer_event(lv_task_t * param);
static void create_clock_page(lv_obj_t* parent);



static void btnm_action(struct _lv_obj_t * obj, lv_event_t event)
{
    uint16_t id;
    if (event == LV_EVENT_SHORT_CLICKED) {
        id = lv_btnm_get_active_btn(obj);
        if (id == 0xffff) {
            return;
        }
        printf("Button: %d released\n", id);
    }
}

/**
 * @brief create Navigation button page
 */
void create_Navigation_button_page(lv_obj_t * parent)
{
    NavigationBtnCtrl.btnm = lv_btnm_create(lv_layer_sys(), NULL);
    lv_btnm_set_map(NavigationBtnCtrl.btnm, btnm_map);
    lv_obj_set_size(NavigationBtnCtrl.btnm, LV_HOR_RES, LV_VER_RES / 4);
    lv_obj_align(NavigationBtnCtrl.btnm, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0); /*Align to the top*/
    lv_obj_set_event_cb(NavigationBtnCtrl.btnm, btnm_action);
}

void update_Status_bar(lv_obj_t * obj)
{
    static char buf[ICON_NUM * 12];
    int offset = 0;
    if (obj != 0) {
        offset += snprintf(buf+offset, sizeof(buf),"%s  ",LV_SYMBOL_BATTERY_FULL);
        for (int i = WIFI_ICON;i< ICON_NUM;i++) {
            offset += snprintf(buf+offset, sizeof(buf) - offset,"%s  ",icon_table[i]);
            if (i % 3 == 0) {
                buf[offset++] = '\n';
            }
        }
        lv_label_set_text(obj, buf);
    }

}

/**
 * @brief create Status bar page
 */
static lv_obj_t * status_bar_obj;
static void create_clock_page(lv_obj_t* parent);

void create_Status_bar_page(lv_obj_t * parent)
{
    lv_coord_t hres = lv_disp_get_hor_res(NULL);
    lv_coord_t vres = lv_disp_get_ver_res(NULL);

    status_bar_obj = lv_obj_create(parent, NULL);
    lv_obj_set_size(status_bar_obj, hres, vres / 6);
    lv_style_copy(&bar_status_style, &lv_style_scr);
    bar_status_style.text.color = LV_COLOR_WHITE;
    bar_status_style.text.sel_color = LV_COLOR_BLACK;
    bar_status_style.body.border.color = LV_COLOR_WHITE;
    bar_status_style.body.main_color = LV_COLOR_BLACK;
    bar_status_style.body.grad_color = LV_COLOR_MAKE(0X23,0X23,0X23);
    bar_status_style.body.shadow.color = LV_COLOR_WHITE;
    lv_obj_set_style(status_bar_obj, &bar_status_style);

    StatusBarCtrl.obj = lv_label_create(status_bar_obj, NULL);
    lv_obj_set_size( StatusBarCtrl.obj, LV_HOR_RES, LV_VER_RES / 4);
    lv_obj_align( StatusBarCtrl.obj, NULL, LV_ALIGN_IN_TOP_LEFT, 3, 0); /*Align to the top*/
    update_Status_bar(StatusBarCtrl.obj);

    create_clock_page(status_bar_obj);
}
static lv_obj_t * holder_page;
#define LV_COLOR_BG LV_COLOR_MAKE(0X32,0X32,0X32)
void create_main_page(void)
{
    lv_coord_t hres = lv_disp_get_hor_res(NULL);
    lv_coord_t vres = lv_disp_get_ver_res(NULL);
    holder_page = lv_obj_create(lv_disp_get_scr_act(NULL), NULL);
    lv_obj_set_size(holder_page, hres, vres);
    lv_style_copy(&main_style, &lv_style_scr);
    main_style.body.main_color = LV_COLOR_BG;
    main_style.body.grad_color = LV_COLOR_BG;
    lv_obj_set_style(holder_page, &main_style);

    create_Status_bar_page(holder_page);

    create_Navigation_button_page(holder_page);

}

static void timer_event(lv_task_t * param);
static void create_clock_page(lv_obj_t* parent)
{
    clock_label = lv_label_create(parent, NULL);
    timer_event(NULL);
    lv_obj_align(clock_label, parent, LV_ALIGN_IN_TOP_LEFT, 120, 0); /*Align to the top*/
    lv_task_create(timer_event, 500, LV_TASK_PRIO_MID, NULL);
}

static void timer_event(lv_task_t * param)
{
    static char msec = 0;
    time_t timer;
    struct tm *tblock;
    time(&timer);
    timer += 8 * 3600;
    tblock = gmtime(&timer);
    msec = !msec;
    lv_label_set_text_fmt(clock_label,
                          "%d/%02d/%02d\n%02d:%02d%c%02d\n",
                          tblock->tm_year+1900, tblock->tm_mon, tblock->tm_mday,
                          tblock->tm_hour, tblock->tm_min, msec?' ':':',tblock->tm_sec);
}
